<?php
require('common.php');

html_header("megumi online");
compile();

function script_links($row)
{
	$domain = $row['Fxn_Call'];
	$name = stripslashes($row['English_Name']);
	$desc = stripslashes($row['Description']);
	if (!empty($desc))
	{
		$desc = ": $desc";
	}
	$uri = "edit_page.php?domain=$domain";
	$execute_uri = $domain . '.php';
	
	// don't display the execute link if this is a form.
	if (strpos($domain, 'form_') !== 0)
	{
		$execute_href = '<a href="' . $execute_uri . '">view</a> the live web page';
		$execute = "<li>$execute_href</li>\n";
		$html .= "<li>" . $name . "$desc</li>\n<ul>";
		$html .= "<li><a href=\"$uri\">edit</a></li>\n$execute\n";

	} else {
		$html .= "<li>(form on this page) " . $name . "$desc</li>\n<ul>";
		$html .= "<li><a href=\"$uri\">edit</a></li>\n";
	}
	$html .= "<li><a href=\"source.php?domain=$domain\">look</a> at the source</li>\n";
	$html .= "<li><a href=\"upload.php?domain=$domain\">download</a> the source</li>\n";
	$html .= "</ul>\n";
	
	return $html;
}

function list_of_scripts()
{
	global $user;
	$html = "<ul>\n";
	
	$result = sql_exec("SELECT * FROM Definitions WHERE User='$user' AND Fxn_Call NOT LIKE 'form%' ORDER BY English_Name");
	while ($row = mysql_fetch_array($result))
	{
		$html .= script_links($row);
		// now look for associated forms
		$domain = $row['Fxn_Call'];
		$assoc_forms = sql_exec("SELECT * FROM Instances WHERE Domain='$domain' AND Fxn_Call='form' ");
		if (mysql_num_rows($assoc_forms) > 0)
		{
			$html .= "<ul>\n";
			while ($assoc_form = mysql_fetch_array($assoc_forms))
			{
				$id = $assoc_form['Var1'];
				$assoc_form_def = sql_row1("SELECT * FROM Definitions WHERE ID='$id' ");
				$html .= script_links($assoc_form_def);
			}
			$html .= "</ul>\n";
		}
	}

	$html .= "</ul>\n";	
	return $html;
}

?>

<body>

<div id="container">

<?php header_div(); ?>

<div id="body">

<p>you can make web pages with this site.  give it a try, since this is a public web site builder of sorts so everyone is welcome to use it, it's free, and you won't break anything.  if you don't like a page, you can delete it anytime.</p>

<p>make a new web page <a href="add_domain.php">here</a></p>

<h4>all the web pages made so far on this site</h4>

<?php echo list_of_scripts(); ?>

<h4>other stuff</h4>

<p><a href="edit_domain.php">edit page settings</a></p>

<?php echo footer(); ?>

</div>
</div>
</body>
</html>
